//
//  SpellClassController.m
//  This file is part of D&D Manager
//
//  Created by Matthew Hutton on 01/05/2007.
//
//
//D&D Manager, a D&D Character and battle management program.
//Copyright (C) 2007, Matthew Hutton
//
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along with this program; if not, write to:
//The Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#import "SpellClassController.h"
#import "SpellMethods.h"

@implementation SpellClassController
-(id)initWithCharController:(NSArrayController *)cCont
		withSpellController:(NSArrayController *)sCont
			withCharTabView:(NSTabView *)tabView{
	[super init];
	//NSLog(@"spell subviews created");

	charController=cCont;
	spellController=sCont;
	characterTabView=tabView;
	[NSBundle loadNibNamed:@"SpellClasses"
					 owner:self];
	return self;
}
-(SpellSubviewController *)currentSubview{
	return currentSubview;
}
-(void)magicViewOpenedForChar:(NSManagedObject *)obj{
	//initial method.
	if(magicViewChanging==YES)
		return;
	magicViewChanging=YES;
	//NSLog(@"magic view started");
	//check how many magic casting classes the character has.
	int count=0;
	BOOL c1=NO,c2=NO,c3=NO;
	if([[[obj valueForKey:@"characterClass1"] valueForKey:@"castsSpells"] boolValue]){
		c1=YES;
		count++;
	}
	if([[[obj valueForKey:@"characterClass2"] valueForKey:@"castsSpells"] boolValue]){
		c2=YES;
		count++;
	}
	if([[[obj valueForKey:@"characterClass3"] valueForKey:@"castsSpells"] boolValue]){
		c3=YES;
		count++;
	}
	NSTabViewItem *magicView=[characterTabView tabViewItemAtIndex:3];
	//NSLog(@"choosing options");
	//NSLog(@"noSpellsView=%@",noSpellsView);
	if(count==0){
		//NSLog(@"no spells");
		[magicView setView:noSpellsView];
	}else if(count==1){
		if(currentSubview==nil){
			//NSLog(@"currentSubview==nil");
			currentSubview=[[SpellSubviewController alloc] initWithCharController:charController																								withSpellController:spellController
																  withCharTabView:characterTabView];
			//NSLog(@"currentSubview=%@",currentSubview);
		}
		NSManagedObject *characterClass;
		if(c1){
			characterClass=[obj valueForKey:@"characterClass1"];
		}else if(c2){
			characterClass=[obj valueForKey:@"characterClass2"];
		}else{
			characterClass=[obj valueForKey:@"characterClass3"];
		}
		[currentSubview processMagicView:magicView
								forClass:characterClass
								 forChar:obj];
	}else if(count==2){
		//NSLog(@"currentSubview1==%@",currentSubview);
		if(currentSubview!=nil){
			[currentSubview release];
		}
		//NSLog(@"currentSubview==nil");
		currentSubview=[[SpellSubviewController alloc] initWithCharController:charController																					  withSpellController:spellController
															  withCharTabView:characterTabView];
		//NSLog(@"currentSubview3==%@",currentSubview);
		//NSLog(@"two spells");
		[magicView setView:twoSpellClassesView];
		//set the labels of the tabs.
		NSArray *array=[twoSpellClassesTab tabViewItems];
		if([array count]<2){
			NSLog(@"not enough objects");
			return;
		}
		if(c1){
			[[array objectAtIndex:0] setLabel:[obj valueForKey:@"charClassName1"]];
			if(c2){
				[[array objectAtIndex:1] setLabel:[obj valueForKey:@"charClassName2"]];
			}else{
				[[array objectAtIndex:1] setLabel:[obj valueForKey:@"charClassName3"]];
			}
		}else{
			[[array objectAtIndex:0] setLabel:[obj valueForKey:@"charClassName2"]];
			[[array objectAtIndex:1] setLabel:[obj valueForKey:@"charClassName3"]];
		}
		//NSLog(@"pre ct");
		//now check which view is visible and deal.
		int currentTab=[twoSpellClassesTab indexOfTabViewItem:[twoSpellClassesTab selectedTabViewItem]];
		//NSLog(@"currentTab=%d",currentTab);
		NSManagedObject *characterClass;
		if(currentTab==0){
			if(c1){
				characterClass=[obj valueForKey:@"characterClass1"];
			}else{
				characterClass=[obj valueForKey:@"characterClass2"];
			}
			//NSLog(@"processMagicView to come");
			//NSLog(@"currentSubview==%@",currentSubview);
			[currentSubview processMagicView:[array objectAtIndex:0]
									forClass:characterClass
									 forChar:obj];
		}else{
			if(c2){
				characterClass=[obj valueForKey:@"characterClass2"];
			}else{
				characterClass=[obj valueForKey:@"characterClass3"];
			}
			//NSLog(@"processMagicView to come");
			//NSLog(@"currentSubview==%@",currentSubview);
			[currentSubview processMagicView:[array objectAtIndex:1]
									forClass:characterClass
									 forChar:obj];
			
		}
	}else{
		if(currentSubview!=nil){
			[currentSubview release];
		}
		//NSLog(@"currentSubview==nil");
		currentSubview=[[SpellSubviewController alloc] initWithCharController:charController																								withSpellController:spellController
															  withCharTabView:characterTabView];
		//NSLog(@"three classes");
		[magicView setView:threeSpellClassesView];
		//set the labels of the tabs.
		NSArray *array=[threeSpellClassesTab tabViewItems];
		if([array count]<3){
			return;
		}
		[[array objectAtIndex:0] setLabel:[obj valueForKey:@"charClassName1"]];
		[[array objectAtIndex:1] setLabel:[obj valueForKey:@"charClassName2"]];
		[[array objectAtIndex:2] setLabel:[obj valueForKey:@"charClassName3"]];
		//now check which view is visible and deal.
		int currentTab=[threeSpellClassesTab indexOfTabViewItem:[threeSpellClassesTab selectedTabViewItem]];
		//NSLog(@"currentTab=%d",currentTab);
		NSManagedObject *characterClass=[obj valueForKey:[NSString stringWithFormat:@"characterClass%d",(currentTab+1)]];
		if(currentTab>=[array count]){
			return;
		}
		[currentSubview processMagicView:[array objectAtIndex:currentTab]
								forClass:characterClass
								 forChar:obj];
	}
	magicViewChanging=NO;
	//NSLog(@"magic view ended");
}
-(void)reloadSpellTables{
	if(currentSubview!=nil){
		[currentSubview reloadSpellTables];
	}
}
- (void)tabView:(NSTabView *)tabView
didSelectTabViewItem:(NSTabViewItem *)tabViewItem{
	if([tabView isEqualTo:twoSpellClassesTab]){
		if([[charController selectedObjects] count]!=0){
			[self magicViewOpenedForChar:[[charController selectedObjects] objectAtIndex:0]];
		}
	}else if([tabView isEqualTo:threeSpellClassesTab]){
		if([[charController selectedObjects] count]!=0){
			[self magicViewOpenedForChar:[[charController selectedObjects] objectAtIndex:0]];
		}
	}
}
@end
